package com.flower.controller.buyer;

import com.flower.service.ShopStatsService;
import com.flower.utils.AjaxResult;
import com.flower.utils.UserContext;
import com.flower.service.ShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * 购买者端商铺数据
 */
@RestController
@RequestMapping("/shop/stats")
@RequiredArgsConstructor
public class ShopStatsController {

    private final ShopStatsService shopStatsService;
    private final ShopService shopService;
    
    // 有效的时间单位列表
    private static final List<String> VALID_TIME_UNITS = Arrays.asList("day", "week", "month");

    /**
     * 获取销售趋势数据
     * @param shopId 店铺ID (可选，不传则使用当前用户的店铺)
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param timeUnit 时间单位：day, week, month
     * @return 销售趋势数据
     */
    @GetMapping("/sales/trend")
    public AjaxResult getSalesTrend(
            @RequestParam(required = false) Integer shopId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(defaultValue = "day") String timeUnit) {
        
        // 验证时间单位
        if (!VALID_TIME_UNITS.contains(timeUnit)) {
            return AjaxResult.error("无效的时间单位，有效值：day, week, month");
        }
        
        // 如果没有传入shopId，自动获取当前用户的店铺ID
        if (shopId == null) {
            Long userId = UserContext.getUserId();
            shopId = shopService.getShopByBuyerId(userId.intValue()).getShopId();
        }
        
        // 如果没有开始日期，默认为30天前
        if (startDate == null) {
            startDate = LocalDate.now().minusDays(30);
        }
        
        // 如果没有结束日期，默认为今天
        if (endDate == null) {
            endDate = LocalDate.now();
        }
        
        return AjaxResult.success(shopStatsService.getSalesTrend(shopId, startDate, endDate, timeUnit));
    }

    /**
     * 获取订单状态分布
     * @param shopId 店铺ID (可选，不传则使用当前用户的店铺)
     * @return 订单状态分布数据
     */
    @GetMapping("/orders/status")
    public AjaxResult getOrderStatusDistribution(@RequestParam(required = false) Integer shopId) {
        // 如果没有传入shopId，自动获取当前用户的店铺ID
        if (shopId == null) {
            Long userId = UserContext.getUserId();
            shopId = shopService.getShopByBuyerId(userId.intValue()).getShopId();
        }
        
        return AjaxResult.success(shopStatsService.getOrderStatusDistribution(shopId));
    }

    /**
     * 获取热销商品排行
     * @param shopId 店铺ID (可选，不传则使用当前用户的店铺)
     * @param limit 返回数量限制
     * @return 热销商品数据
     */
    @GetMapping("/products/top")
    public AjaxResult getTopProducts(
            @RequestParam(required = false) Integer shopId,
            @RequestParam(defaultValue = "10") Integer limit) {
        
        // 参数验证
        if (limit <= 0 || limit > 100) {
            limit = 10; // 使用默认值
        }
        
        // 如果没有传入shopId，自动获取当前用户的店铺ID
        if (shopId == null) {
            Long userId = UserContext.getUserId();
            shopId = shopService.getShopByBuyerId(userId.intValue()).getShopId();
        }
        
        return AjaxResult.success(shopStatsService.getTopProducts(shopId, limit));
    }

    /**
     * 获取商品分类销售分布
     * @param shopId 店铺ID (可选，不传则使用当前用户的店铺)
     * @return 分类销售分布数据
     */
    @GetMapping("/categories/distribution")
    public AjaxResult getCategoryDistribution(@RequestParam(required = false) Integer shopId) {
        // 如果没有传入shopId，自动获取当前用户的店铺ID
        if (shopId == null) {
            Long userId = UserContext.getUserId();
            shopId = shopService.getShopByBuyerId(userId.intValue()).getShopId();
        }
        
        return AjaxResult.success(shopStatsService.getCategoryDistribution(shopId));
    }
} 